﻿#include <easyx.h>
#include <stdio.h>
#include <stdlib.h>


//struct Rect
//{
//	int left;
//	int top;
//	int right;
//	int bottom;
//	void (*draw)(struct Rect*);//封装
//};
//
//struct Circle
//{
//	int x;
//	int y;
//	int r;
//	void (*draw)(struct Circle*);//封装
//};
//
//struct Triangle
//{
//	POINT p1;
//	POINT p2;
//	POINT p3;
//	void (*draw)(struct Triangle*);//封装
//};

//struct Shape
//{
//	void(*draw)(struct Shape*);//共性方法
//};
//
//struct Rect
//{
//	struct Shape super;
//	int left;
//	int top;
//	int right;
//	int bottom;
//};
//
//struct Circle
//{
//	struct Shape super;
//	int x;
//	int y;
//	int r;
//};
//
//struct Triangle
//{
//	struct Shape super;
//	POINT p1;
//	POINT p2;
//	POINT p3;
//};
//
//void drawRect(struct Rect* r)
//{
//	rectangle(r->left, r->top, r->right, r->bottom);
//}
//
//void drawCircle(struct Circle* c)
//{
//	circle(c->x, c->y, c->r);
//}
//
//void drawTriangle(struct Triangle* t)
//{
//	line(t->p1.x, t->p1.y, t->p2.x, t->p2.y);
//	line(t->p2.x, t->p2.y, t->p3.x, t->p3.y);
//	line(t->p3.x, t->p3.y, t->p1.x, t->p1.y);
//}
//
//void initRect(struct Rect* r)
//{
//	r->super.draw = (void (*)(struct Shape*))drawRect;//左侧参数为void (*)(struct Shape*),而右边类型分别为void (*)(struct Rect*)
//}
//
//void initCircle(struct Circle* c)//函数内部类型不一致，无法进行复制，可以强制进行转换
//{
//	c->super.draw = (void (*)(struct Shape*))drawCircle;
//}
//
//void initTriangle(struct Triangle* t)
//{
//	t->super.draw = (void (*)(struct Shape*))drawTriangle;
//}
//
//int main()
//{
//	initgraph(800, 600);
//	setaspectratio(1, -1);
//	setorigin(400, 300);
//
//	setbkcolor(WHITE);
//	setlinecolor(BLACK);
//	cleardevice();
//
//	struct Rect r = { {}, - 200,200,200,0 };
//	struct Circle c = { {},0,0,100 };
//	struct Triangle t = { {}, {0,200},{-200,0},{200,0} };
//	initRect(&r);
//	initCircle(&c);
//	initTriangle(&t);
//	struct Shape* arrShape[3] = { (struct Shape*)&r,(struct Shape*)&c,(struct Shape*)&t };//结构体数组
//	for (int i = 0; i < 3; i++)
//	{
//		arrShape[i]->draw(arrShape[i]);//强制转换后，虽然调用的是shape里的draw函数，但是在数组里强制类型转换，所以可以实现同样效果
//	}
//
//	getchar();
//	return 0;
//}